using UnityEngine;

public class BallCon : MonoBehaviour
{
    private Rigidbody2D _rigidbody2D;
    private readonly float _speed = 16f;

    void Start()
    {
        _rigidbody2D = GetComponent<Rigidbody2D>();
        Invoke(nameof(SetRandomTrajectory), 1f);
    }

    public void ResetPosion(GameObject ballPrefabs)
    {
        GameObject.Instantiate(ballPrefabs, Vector3.zero, Quaternion.identity);
    }

    private void SetRandomTrajectory()
    {
        _ = Vector2.zero;
        Vector2 direction;
        // 确保初始方向向下且非垂直
        do
        {
            direction = Random.insideUnitCircle.normalized;
        } while (Mathf.Abs(direction.y) < 0.1f); // 避免接近垂直的角度

        // 使用 Impulse 模式施加力
        _rigidbody2D.AddForce(direction * _speed, ForceMode2D.Impulse);
    }
}